package com.meihua.mes2ehr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meihua.mes2ehr.constants.FSConstants;
import com.meihua.mes2ehr.entity.*;
import com.meihua.mes2ehr.mapper.FsMesSheetMapper;
import com.meihua.mes2ehr.service.FsMesPointTargetService;
import com.meihua.mes2ehr.mapper.FsMesPointTargetMapper;
import com.meihua.mes2ehr.service.FsMesSheetService;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author maheqiang
* @description 针对表【fs_mes_point_target】的数据库操作Service实现
* @createDate 2024-04-21 13:33:44
*/
@Service
public class FsMesPointTargetServiceImpl extends ServiceImpl<FsMesPointTargetMapper, FsMesPointTarget>
    implements FsMesPointTargetService{
    @Resource
    private FsMesSheetMapper fsMesSheetMapper;
    @Override
    public void getMesTargetFromFsSheet(String sheetId) {
        this.remove(null);
//        获取飞书token
        String res = HttpUtil.post(FSConstants.FS_API_URL, (String) null);
        String tenantAccessToken = JSON.parseObject(res, FsTokenDTO.class).getTenant_access_token();
//        获取表格信息
        String result = HttpUtil.createGet(FSConstants.FS_TABLE_API_URL)
                .header("Authorization", "Bearer " + tenantAccessToken)
                .execute()
                .body();
        System.out.println(result);
        FsTableDataDTO data = JSON.parseObject(result, FsTableResult.class).getData();

        String objToken = data.getNode().getObjToken();
        String url = FSConstants.FS_SHEET_API_URL + objToken + "/values/" + sheetId;

        String messageResult = HttpUtil.createGet(url)
                .header("Authorization", "Bearer " + tenantAccessToken)
                .execute()
                .body();
        List<List<String>> values = JSON.parseObject(messageResult, FsMesMessageResult.class).getData().getValueRange().getValues();
        ArrayList<FsMesPointTarget> fsMesPoints = new ArrayList<>();
        for (int i = 1; i < values.size(); i++) {
            FsMesPointTarget fsMesPointTarget = new FsMesPointTarget();
            fsMesPointTarget.setProductLine(values.get(i).get(0));
            fsMesPointTarget.setWorkshopName(values.get(i).get(1));
            fsMesPointTarget.setTarget(values.get(i).get(2));
            fsMesPointTarget.setAssessmentClassification(values.get(i).get(3));
            fsMesPointTarget.setAssessmentCycle(values.get(i).get(4));
            fsMesPointTarget.setDatasource(values.get(i).get(5));
            fsMesPointTarget.setMesPointNo(values.get(i).get(6));
            fsMesPointTarget.setLarkSheetField(values.get(i).get(7));
            fsMesPointTarget.setDataApproveProcession(values.get(i).get(8));
            fsMesPointTarget.setDataFeedbackRate(values.get(i).get(9));
            fsMesPointTarget.setDataFormart(values.get(i).get(10));
            fsMesPointTarget.setDataSupportStandard(values.get(i).get(11));
            fsMesPointTarget.setTargetStatus(values.get(i).get(15));
            fsMesPointTarget.setSheetId(sheetId);
            fsMesPointTarget.setTargetId(sheetId+"_"+i);
            fsMesPoints.add(fsMesPointTarget);
        }
        this.saveBatch(fsMesPoints);
    }
}




